from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class OrderDetailsPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.order_id_locator = (By.ID, "order-id")
        self.order_date_locator = (By.ID, "order-date")
        self.order_status_locator = (By.ID, "order-status")
        self.order_total_locator = (By.ID, "order-total")
        self.items_list_locator = (By.ID, "items-list")
        self.shipping_address_locator = (By.ID, "shipping-address")
        self.billing_address_locator = (By.ID, "billing-address")
        self.payment_method_locator = (By.ID, "payment-method")
        self.cancel_order_button_locator = (By.ID, "cancel-order-button")
        self.return_item_button_locator = (By.ID, "return-item-button")

    def navigate(self, order_id):
        self.driver.get(f'http://example.com/orders/{order_id}')

    def get_order_id(self):
        order_id = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_id_locator)
        )
        return order_id.text

    def get_order_date(self):
        order_date = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_date_locator)
        )
        return order_date.text

    def get_order_status(self):
        order_status = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_status_locator)
        )
        return order_status.text

    def get_order_total(self):
        order_total = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.order_total_locator)
        )
        return order_total.text

    def get_items_list(self):
        items_list = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.items_list_locator)
        )
        return items_list

    def get_shipping_address(self):
        shipping_address = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.shipping_address_locator)
        )
        return shipping_address.text

    def get_billing_address(self):
        billing_address = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.billing_address_locator)
        )
        return billing_address.text

    def get_payment_method(self):
        payment_method = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.payment_method_locator)
        )
        return payment_method.text

    def click_cancel_order(self):
        cancel_order_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.cancel_order_button_locator)
        )
        cancel_order_button.click()

    def click_return_item(self):
        return_item_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.return_item_button_locator)
        )
        return_item_button.click()